<?php

namespace App\Http\Controllers\Api;

use App\Services\WeatherService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CityWeatherController extends Controller
{
    public function index(Request $request)
    {
        $RequestId = $request->RequestId;
        $city = $request->SlotInfoList[0]['SlotValue'];
        $weather = new WeatherService();
        $weather = $weather->getWeather($city, 'all');
        $data = '';
        if ($weather['info'] == "OK") {
            $data .= $city.':';
            foreach ($weather['forecasts'][0]['casts'] as $k => $item) {
                if ($k < 3) {
                    $data .= $item['date'] . ",星期" . $item['week'] . ',白天天气:' . $item['dayweather'] . ',白天温度:' . $item['daytemp'] . ',晚上天气:' . $item['nightweather'] . ",晚上温度：" . $item['nighttemp'] . ";";
                }
            }

        } else {
            $data .= "气象台忙，请稍后在尝试";
        }
        $array = [
            'RequestId'       => $RequestId,
            'ResponseMessage' => [
                "ContentType" => "PlainText",
                "Content"     => $data,
            ],
        ];
        return json_encode($array, true);
    }
}
